package com.bawei.mdm2207alast.common

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.route.RouteConfig
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV

/**
 * 作者：赵亮
 * 时间：2024/12/16 16:04
 * 功能：通用组件封装
 */

/**
 * 用户信息卡片组件
 * @param navController 导航
 * @param mmkv 键值对存储
 */
@Composable
fun UserCard(navController: NavController, mmkv: MMKV) {
    // 用户名
    var username by remember {
        mutableStateOf(mmkv.decodeString("username", "未登录").toString())
    }

    // 角色 1.员工 2.经理
    var role by remember {
        mutableStateOf(mmkv.decodeInt("role", 0))
    }

    // 是否显示退出对话框
    var isShow by remember {
        mutableStateOf(false)
    }

    // 用户信息 卡片
    Card(
        elevation = CardDefaults.cardElevation(10.dp),
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                // 判断时去登录，还时退出
                if (username == "未登录") {
                    // 跳转到登录页面
                    navController.navigate(RouteConfig.LOGIN)
                } else {
                    // 弹窗退出
                    isShow = true
                }
            },
        colors = CardDefaults.cardColors(containerColor = Color.White),
        border = BorderStroke(1.dp, color = Color.Black)
    ) {
        if (isShow) {
            AlertDialog(
                onDismissRequest = { },
                title = { Text(text = "退出提示！") },
                text = { Text(text = "确定要退出吗？") },
                dismissButton = {
                    Text(text = "取消", modifier = Modifier.clickable {
                        isShow = false
                    })
                },
                confirmButton = {
                    Text(text = "确认", modifier = Modifier.clickable {
                        mmkv.clearAll()
                        username = "未登录"
                        role = 0
                        ToastUtils.showShort("退出车工！")
                        isShow = false
                    })
                })
        }

        Row(modifier = Modifier.padding(15.dp)) {
            Image(
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape),
                painter = painterResource(
                    id = R.drawable.profile_picture
                ), contentDescription = null
            )

            Column(
                modifier = Modifier
                    .height(80.dp)
                    .padding(start = 10.dp, top = 10.dp, bottom = 10.dp),
                verticalArrangement = Arrangement.SpaceBetween
            ) {
                Text(text = username)
                Text(
                    text = listOf("", "员工", "经理")[role]
                )
            }

        }

    }
}

/**
 * 标题组件
 */
@Composable
fun BaseTitle(navController: NavController, title: String) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 24.dp)
    ) {
        Text(text = title, modifier = Modifier.align(Alignment.Center))

        Icon(
            modifier = Modifier
                .padding(start = 15.dp)
                .clickable {
                    // 返回
                    navController.popBackStack()
                },
            imageVector = Icons.Default.ArrowBack,
            contentDescription = null
        )
    }
}